<template>
    <div class="background">
        <el-card class="box-card">
            <h1 style="margin-top:-2%;text-align: center;">欢迎来到fscummer</h1>
            <div>
                <el-form ref="loginform" :model="loginform" label-width="80px" :rules="rules">
                    <el-form-item label="用户名" prop="username">
                        <el-input v-model="loginform.username" name="username"></el-input>
                    </el-form-item>
                    <el-form-item label="密码" prop="password">
                        <el-input v-model="loginform.password" show-password name="password"></el-input>
                    </el-form-item>
                    <el-form-item label="记住我">
                        <el-switch v-model="loginform.RememberMe" active-color="#13ce66" inactive-color="#ff4949"
                            name="remeber-me">
                        </el-switch>
                    </el-form-item>
                </el-form>
                <div class="loginAndRegister">
                    <el-button type="primary" round @click="handleLogin" :loading="loading">点击登录</el-button>
                    &nbsp; &nbsp; &nbsp;
                    <el-link type="primary" href="https://api.weibo.com/oauth2/authorize?client_id=2799832358&response_type=code&redirect_uri=https://blogfbk.com:8081/#/">微博登录</el-link>
                    <vcode ref="vcode" />
                    &nbsp; &nbsp; &nbsp;
                    <el-link type="warning" @click="goRegister">还未登录?点击注册</el-link>
                </div>
            </div>
        </el-card>
    </div>
</template>

<script>
import vcode from '@/component/vcode.vue'
export default {

    data() {
        var usernameValidator = (rule, value, callback) => {
            if (value === '') {
                return callback(new Error('用户名不能为空'))
            } else {
                callback()
            }
        };
        var passwordValidator = (rule, value, callback) => {
            if (value === '') {
                return callback(new Error('密码不能为空'))
            } else {
                callback()
            }
        }
        return {
            loginform: {
                username: '',
                password: '',
                RememberMe: true
            },
            loading: false,
            redirect: undefined,
            rules: {
                username: [
                    { validator: usernameValidator, trigger: 'blur' }
                ],
                password: [
                    { validator: passwordValidator, tigger: 'blur' }
                ]
            }
        }
    },
    components: {
        vcode
    },
    watch: {
        $route: {
            handler: function (route) {
                this.redirect = route.query && route.query.redirect
            },
            immediate: true
        }
    },
    methods: {
        goRegister() {
            this.$router.push({
                name: 'register'
            })
        },
        handleLogin() {
            console.log('form', this.loginform)
            this.$refs.vcode.isShow = true
            this.loading = true
            this.$refs.vcode.loginform.username = this.loginform.username
            this.$refs.vcode.loginform.password = this.loginform.password
            this.$refs.vcode.loginform.RememberMe = this.loginform.RememberMe
        },
    },
}
</script>

<style scoped>
.background {
    margin: 0px;
    padding: 0px;
    background: url(../assets/img/loginbackground.jpg) no-repeat;
    position: fixed;
    background-size: 100%, 100%;
    width: 100%;
    height: 100%;
    background-position: center bottom;
}

.box-card {
    position: fixed;
    left: 35%;
    width: 30%;
    top: 35%;
    opacity: 0.9;
}

.loginAndRegister {
    display: flex;
    justify-content: center;
}
</style>